package com.mogu.user.service.impl;


import com.mogu.entity.TBlog;
import com.mogu.entity.TUser;
import com.mogu.user.mapper.UserMapper;
import com.mogu.user.pojo.Email;
import com.mogu.user.pojo.MyBlog;
import com.mogu.user.pojo.MyComment;
import com.mogu.user.service.EmailService;
import com.mogu.user.service.UserService;
import com.mogu.user.utils.RedisUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    EmailService emailService;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Override
    public String regist(TUser tUser) {

        int i = userMapper.selectUserByUsername(tUser.getUsername());
        if (i==1){
            return "用户名重复，请重新输入";
        }

        String str=RandomStringUtils.randomAlphanumeric(10);
        tUser.setUid(str);

        if(redisTemplate.opsForValue().get(tUser.getMobile()) ==null) {
            return "验证码不存在或者过期";
        }
        String code = redisTemplate.opsForValue().get(tUser.getMobile());
        if(!code.equals(tUser.getCode())){
            return "验证码错误";
        }
            userMapper.insertUser(tUser);
            return "验证码正确";
//        String userName = tUser.getUserName();
//
//
//        String str=RandomStringUtils.randomAlphanumeric(10);
//        tUser.setUid(str);
//
//        TUser byUserName = userMapper.findByUserName(userName);
//
//
//        if (byUserName!=null){
//            return "用户名不唯一";
//        }
//
//        String code = tUser.getValidCode();
//
//        String o =(String) redisUtils.get(tUser.getEmail());
//
//        if (code.equals(o)){
//
//            //存入数据库
//            userMapper.insertUser(tUser);
//        }else {
//            return "验证码不对，重新发送";
//        }
//
//        return "success";

    }

    @Override
    public String sends(Map<String, String> map) {
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {

            int r = random.nextInt(10); //每次随机出一个数字（0-9）

            code = code + r;  //把每次随机出的数字拼在一起

        }

        Email email = new Email();
        email.setSubject("注册发送验证码");
        email.setMessage(code);
        email.setSendTo(map.get("email"));

        //调用发送邮件的服务  发送验证码  返回成功后 存入redis
        String s = emailService.send(email);
        if (s.equals("success")){
            redisUtils.set(email.getSendTo(),email.getMessage());
            return "success";

        }else
            return "fail";
    }

    @Override
    public String login(TUser tUser) {

        //String userName = tUser.getUserName();
        //String passWord = tUser.getPassWord();
        //System.out.println("服务层"+tUser.getUserName());
        //System.out.println("服务层"+tUser.getPassWord());


//        TUser byUserName = userMapper.findByUserName(tUser.getUsername());
//        if (byUserName.getUsername()==null){
//            return "用户不存在";
//        }
        //System.out.println("后端密码"+byUserName.getPassWord());
//        if (byUserName.getPassword().equals(tUser.getPassword())){
//            UUID uuid = UUID.randomUUID();
//            redisUtils.set(uuid.toString(),byUserName);
//            return uuid.toString();
//
//        }
//
//        return "登录失败";
//        }
        int i = userMapper.selectUserByUsername(tUser.getUsername());
        if (i==0){
            return "用户名不存在";
        }
        int a = userMapper.selectUserByUsernameAndPassword(tUser.getUsername(),tUser.getPassword());
        if (a==0){
            return "密码错误，请重新输入";
        }
            UUID uuid = UUID.randomUUID();
            redisUtils.set(uuid.toString(),tUser.getUsername());

        return uuid.toString();

    }

    @Override
    public TUser selectOne(String username) {
        return userMapper.selectOne(username);
    }

    @Override
    public String edit(TUser tUser) {
        userMapper.editUser(tUser);
        return "修改成功";
    }

    @Override
    public List<MyComment> findMyCommentByUid(String uid) {
        return this.userMapper.findMyCommentByUid(uid);
    }

    @Override
    public List<MyBlog> findMyBlogByUid(String uid) {


        return this.userMapper.findMyBlogByUid(uid);
    }


}
